package org.zhq.service.impl.center;

import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.mapper.UsersMapper;
import org.zhq.pojo.Users;
import org.zhq.pojo.bo.center.CenterUserBO;
import org.zhq.service.center.CenterUsersService;

import java.util.Date;

@Service
public class CenterUsersServiceImpl implements CenterUsersService {

    private final UsersMapper usersMapper;

    @Autowired
    public CenterUsersServiceImpl(UsersMapper usersMapper) {
        this.usersMapper = usersMapper;
    }

    /**
     * 根据用户id 查询用户
     *
     * @param userId
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUsersInfoById(String userId) {
        Users users = usersMapper.selectById(userId);
        users.setPassword(null);
        return users;
    }

    /**
     * 修改用户信息
     * @param userId
     * @param centerUserBO
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public Users updateUserInfo(String userId, CenterUserBO centerUserBO) {
        Users updateUser = new Users();
        BeanUtil.copyProperties(centerUserBO,updateUser);
        updateUser.setId(userId);
        updateUser.setUpdatedTime(new Date());
        usersMapper.updateById(updateUser);

        return usersMapper.selectById(userId);
    }
}
